package cc.autoapi.pucong.auto.flow.spring.boot.start;

import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.send.AbstractAiSendSendTool;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.send.handler.Kimi32kAiHandlerSend;
import cc.autoapi.pucong.auto.flow.core.root.properties.AutoFlowDesignConfiguration;
import cc.autoapi.pucong.auto.flow.core.root.properties.AutoFlowDesignProperties;
import cc.autoapi.pucong.auto.flow.persistence.repository.api.AutoFlowAuthInterceptor;
import cc.autoapi.pucong.auto.flow.persistence.repository.api.AutoFlowAuthInterceptorDefault;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
@ComponentScan("cc.autoapi.pucong.auto.flow")
@ConditionalOnClass({RequestMappingHandlerMapping.class})
@EnableConfigurationProperties(AutoFlowDesignProperties.class)
public class AutoServiceAutoConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(AutoServiceAutoConfiguration.class);

    private final AutoFlowDesignProperties properties;

    private final Environment environment;

    private final ApplicationContext applicationContext;


    @Value("${server.servlet.context-path}")
    private String systemPath;

    @Value("${server.port}")
    private Integer systemPort;

    public AutoServiceAutoConfiguration(AutoFlowDesignProperties autoFlowDesignProperties,
                                        Environment environment,
                                        ApplicationContext applicationContext) {
        this.properties = autoFlowDesignProperties;
        this.environment = environment;
        this.applicationContext = applicationContext;
    }

    @Bean
    @ConditionalOnMissingBean
    public AutoFlowDesignConfiguration liteFlowDesignConfiguration() {
        AutoFlowDesignConfiguration configuration = new AutoFlowDesignConfiguration();
        configuration.setEnable(properties.isEnable());
        configuration.setWeb(properties.getWeb());
        configuration.setJdbcConfig(properties.getJdbcConfig());
        configuration.setSecurityConfig(properties.getSecurityConfig());
        configuration.setSupportCrossDomain(properties.isSupportCrossDomain());
        // 打印banner
        if (properties.isBanner() && properties.isEnable()) {
            configuration.printBanner(systemPath, systemPort);
        }
        return configuration;
    }

    @Bean
    @ConditionalOnMissingBean
    public AbstractAiSendSendTool createAiTool() {
        return new Kimi32kAiHandlerSend(properties.getAiConfig().getKimiKey());
    }

    @Bean
    @ConditionalOnMissingBean
    public AutoFlowAuthInterceptor createAutoFlowExecFitter() {
        return new AutoFlowAuthInterceptorDefault(properties.getSecurityConfig().getUsername(), properties.getSecurityConfig().getPassword());
    }

}
